package com.sso.action.webserice;

import com.alibaba.fastjson.JSONObject;
import com.sso.action.BaseAction;
import com.sso.common.ConstantDate;
import com.sso.common.enums.FlagStatus;
import com.sso.entity.SysAccessControl;
import com.sso.exception.SelectException;
import com.sso.manager.SysAccessControlManager;
import com.sso.manager.SysUserManager;
import com.tools.util.JSONUtils;
import com.tools.util.RSAUtils;
import com.tools.util.RequestUtils;
import com.tools.util.StrUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登陆接口
 */
@Controller
@RequestMapping("/ws/login")
public class LoginWebService extends BaseAction {

    @Autowired
    private SysAccessControlManager operationSystemControlManager;
    @Autowired
    private SysUserManager userManager;

    @RequestMapping("login.do")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取参数
            JSONObject param = RequestUtils.getInputStreamToJSON(request.getInputStream());
            //获取访问系统标识
            String sysEname = this.getSysEname(request);
            //获取私钥地址
            String keyPath = this.getRSAPrivateKeyPath(sysEname);
            //判断私钥
            if(StrUtils.isEmpty(keyPath)){
                JSONUtils.outputJson(ConstantDate.InterfaceStatus.SYS_KEY_ERROR,"密钥异常，请联系管理人员",response);
                return;
            }
            //参数解密
            String username = RSAUtils.decrypt(keyPath, (String) param.get("username"));
            String password = RSAUtils.decrypt(keyPath, (String) param.get("password"));
            //判断参数有效性
            if(StrUtils.isEmpty(username)){
                this.outputJson(ConstantDate.InterfaceStatus.USERNAME_ERROR,"请输入用户名",response);
                return;
            }
            if(StrUtils.isEmpty(password)){
                this.outputJson(ConstantDate.InterfaceStatus.PASSWORD_ERROR,"请输入密码",response);
                return;
            }
            //用户登陆
            String status = userManager.login(username,password);
            //判断登陆状态
            if(FlagStatus.SUCCESS.value.equals(status)){
                //加密token
                String token = RSAUtils.encrypt(ConstantDate.DESPassword.SSO_TOKEN,request.getSession().getId());
                this.outputJson(ConstantDate.InterfaceStatus.RETURN_SUCCESS,token,response);
            }else{
                //返回错误信息
                this.outputJson(ConstantDate.InterfaceStatus.LOGIN_ERROR,status,response);
            }
        } catch (IOException e) {
            e.printStackTrace();
            this.outputJson(ConstantDate.InterfaceStatus.SYS_ACCESS_ERROR,"通讯异常",response);
        }
    }

    @RequestMapping("isLogin")
    public void isLogin(HttpServletRequest request, HttpServletResponse response) {
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
        }
    }

    /**
     * 获取用户的私钥路径
     * @param sysEname
     * @return
     */
    private String getRSAPrivateKeyPath(String sysEname){
        try {
            SysAccessControl operationSystemControl = operationSystemControlManager.selectBySysEname(sysEname);
//            return operationSystemControl.getPrivateKeyPath();
            return "F:/temp/pri.key";
        } catch (SelectException e) {
            e.printStackTrace();
            return "";
        }
    }

    private String getRSAPublicKeyPath(String sysEname){
        try {
            SysAccessControl operationSystemControl = operationSystemControlManager.selectBySysEname(sysEname);
//            return operationSystemControl.getPrivateKeyPath();
            return "F:/temp/pub.key";
        } catch (SelectException e) {
            e.printStackTrace();
            return "";
        }
    }

    private String getSysEname(HttpServletRequest request){
        return (String) request.getSession().getAttribute("sysEname");
    }
    private boolean isLogin(HttpServletResponse response){
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            JSONUtils.outputJson(ConstantDate.InterfaceStatus.LOGIN_ERROR,"您还未登陆或已登陆超时，请重新登陆",response);
            return false;
        }
        return true;
    }
}
